package com.quanyan.place.component;

import com.alibaba.dubbo.common.logger.Logger;
import com.alibaba.dubbo.common.logger.LoggerFactory;
import com.quanyan.place.biz.ISearchService;
import com.quanyan.place.mail.MasterStatInfo;
import com.quanyan.place.service.BusinessAwardService;
import com.quanyan.place.service.PlaceTaskService;
import com.quanyan.place.wrapper.PlaceUnitSalePlanWrapper;
import org.apache.commons.lang3.time.FastDateFormat;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;

/**
 * Created by Kingson.chan on 16/6/03.
 */

//@Component("scheduledTask")
public class ScheduledTask {

    private static Logger logger = LoggerFactory.getLogger(ScheduledTask.class);
    public static final FastDateFormat dateFormat = FastDateFormat.getInstance("yyyy-MM-dd HH:mm:ss");
    private Integer expiredOrderCheckTaskCount = 1;
    private Integer expiredSalePlanCheckTaskCount = 1;
    private Integer orderCompleteStatusCheckTaskCount = 1;

    @Autowired
    private PlaceTaskService placeTaskService;

    @Autowired
    private BusinessAwardService businessAwardService;

    @Autowired
    private PlaceUnitSalePlanWrapper salePlanWrapper;

    @Autowired
    private ISearchService searchService;


    /**
     * 定期查询未付款过期状态订单，释放库存
     */
    //@Scheduled(cron = "0 0/3 * * * ?")
    public void expiredOrderCheckTask()  {
        logger.warn(String.format("定期查询过期的订单，第%s次执行，当前时间为：%s", expiredOrderCheckTaskCount++, dateFormat.format(new Date())));
        try {
            placeTaskService.expiredOrderCheckTask();
        }catch (Exception e){
            logger.warn(String.format("第%s次执行，当前时间为：%s", expiredOrderCheckTaskCount++, dateFormat.format(new Date())));
        }

    }

    /**
     *定期检查锁表时间过期的销售计划，释放销售计划库存
     */
    //@Scheduled(cron = "0 0/3 * * * ?")
    public void expiredSalePlanCheckTask(){
        logger.warn(String.format("定期释放销售计划库存，第%s次执行，当前时间为：%s", expiredSalePlanCheckTaskCount++, dateFormat.format(new Date())));
        try {
            placeTaskService.expiredSalePlanCheckTask();
        }catch (Exception e){
            logger.warn(String.format("第%s次执行，当前时间为：%s", expiredSalePlanCheckTaskCount++, dateFormat.format(new Date())));
        }
    }


    /**
     *将活动完成的订单状态设置为已完成状态
     */
    //@Scheduled(cron = "0 0 0/1 * * ?")
    public void orderCompleteStatusCheckTask(){
        logger.warn(String.format("订单交易完成检查，第%s次执行，当前时间为：%s", orderCompleteStatusCheckTaskCount++, dateFormat.format(new Date())));
        try {
            placeTaskService.orderCompleteStatusCheckTask();
        }catch (Exception e){
            logger.warn(String.format("第%s次执行，当前时间为：%s", orderCompleteStatusCheckTaskCount++, dateFormat.format(new Date())));
        }
    }

    /**
     * 同步销售计划（每天23点执行）
     */
    //@Scheduled(cron = "0 0 23 * * ?")
    public void syncPlaceSalePlan(){
        logger.warn("定时同步销售计划开启");
        try {
            salePlanWrapper.syncPlaceSalePlanScheduled();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 商务代表指标统计
     */
    //@Scheduled(cron = "0 0 0 1 * ?")
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void agentStatisTask(){
        logger.warn(String.format("商务代表统计指标统计，当前时间为：%s",dateFormat.format(new Date())));
        try {
            businessAwardService.insertAgentStatis();
        }catch (Exception e){
            logger.warn(String.format("商务代表统计指标统计，当前时间为：%s", dateFormat.format(new Date())));
        }
    }

    /**
     * 商务代表奖金计算
     */
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void agentStatisAwardTask(){
        logger.warn(String.format("商务代表统计绩效统计，当前时间为：%s",dateFormat.format(new Date())));
        try {
            businessAwardService.insertAgentAward();
        }catch (Exception e){
            System.out.print("================"+e.getMessage());
            logger.warn(String.format("商务代表统计绩效统计，当前时间为：%s",dateFormat.format(new Date())));
        }
    }

    /**
     * 商务主管奖金计算
     */
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void managerAwardTask(){
        logger.warn(String.format("商务主管统计绩效统计，当前时间为：%s",dateFormat.format(new Date())));
        try {
            businessAwardService.insertBusinessManagerAward();
        }catch (Exception e){
            System.out.print("================"+e.getMessage());
            logger.warn(String.format("商务主管统计绩效统计，当前时间为：%s",dateFormat.format(new Date())));
        }
    }

    /**
     * 城市经理奖金计算
     */
    //@Scheduled(cron = "0 0/2 * * * ?")
    public void cityManagerAwardTask(){
        logger.warn(String.format("城市经理奖金绩效统计，当前时间为：%s",dateFormat.format(new Date())));
        try {
            businessAwardService.insertCityManagerAward();
        }catch (Exception e){
            System.out.print("================"+e.getMessage());
            logger.warn(String.format("城市经理奖金绩效统计，当前时间为：%s",dateFormat.format(new Date())));
        }
    }


    /**
     * 每天更新场馆索引
     */
    //@Scheduled(cron = "0 0 0 * * ?")
    public void updatePlaceIndex(){
        /*if (searchService.delAllDoc()){
            logger.debug("删除所有场馆索引成功");
//            if(this.searchService.importPlaceIndexDays(PlaceConstants.SOLR_INDEX_DAYS)){
            if(this.searchService.delIndexBefore14Day()){
                logger.debug("导入所有场馆索引成功");
            }else {
                logger.warn(String.format("导入所有场馆索引失败，当前时间为：%s",dateFormat.format(new Date())));
            }
        }else {
            logger.warn(String.format("删除所有场馆索引失败，当前时间为：%s",dateFormat.format(new Date())));
        }*/
        if(this.searchService.delIndexBefore14Day()){
            logger.debug(String.format("删除过期场馆索引成功，当前时间为：%s",dateFormat.format(new Date())));
        }else {
            logger.warn(String.format("删除过期场馆索引失败，当前时间为：%s",dateFormat.format(new Date())));
        }
    }


    //@Scheduled(cron = "0 0 10 * * ?")
    public void masterStatMailTask(){
        System.out.println("达人今日未提现未付款金额邮件发送");
        try {
            MasterStatInfo.masterStatMailSend();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
